package com.harold.jstest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.harold.base.JSKit;
/**
 * 主界面
 *****************************************************
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>主界面</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2014-8-27 上午11:36:53</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>CorpIt</dd>
 * @author aa1000777 - Email:aa1000777@qq.com
 *****************************************************
 */
public class MainActivity extends Activity {
	
	private WebView mWebView;
	private Button btnShowInfo;
	private JSKit js;
	private Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//初始化控件
		mWebView = (WebView) findViewById(R.id.wv_test);
		btnShowInfo = (Button) findViewById(R.id.btn_showmsg);
		//实例化js对象
		js = new JSKit(this);
		//设置参数
		mWebView.getSettings().setBuiltInZoomControls(true);
		//内容的渲染需要webviewChromClient去实现，设置webviewChromClient基类，解决js中alert不弹出的问题和其他内容渲染问题
		mWebView.setWebChromeClient(new WebChromeClient());
		mWebView.getSettings().setJavaScriptEnabled(true);
		//把js绑定到全局的myjs上，myjs的作用域是全局的，初始化后可随处使用
		mWebView.addJavascriptInterface(js, "myjs");
		
		mWebView.loadUrl("file:///android_asset/test.html");
		
		btnShowInfo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						//调用 HTML 中的javaScript 函数
						mWebView.loadUrl("javascript:showMsg(200,0)");
					}
				});
			}
		});
		
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView webview, String url) {
				webview.loadUrl(url);
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				mWebView.loadUrl("javascript:showMsg(200,0)");
				super.onPageStarted(view, url, favicon);
			}
			
		});
	}

}
